﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using System.Text.RegularExpressions;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Tuzzit_Modele;

namespace Tuzzit
{
    public partial class Profil : PhoneApplicationPage
    {
        public Profil()
        {
            InitializeComponent();
            GetProjects();
        }

        private void GetProjects()
        {

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://dev.tuzzit.com/exec/user_controller.php?function=get_projects_for_user&format=json");
            request.CookieContainer = new CookieContainer();
            request.CookieContainer.Add(new Uri("http://dev.tuzzit.com"), (Application.Current as App).Cookies);
            


          
           request.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), request);

            //test


        }

        private void ReadWebRequestCallback(IAsyncResult callbackResult)
        {
        HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult);
            
            (Application.Current as App).Cookies = myResponse.Cookies;


                   /*     foreach (Cookie ck in myResponse.Cookies)
                        {
                            

                            Dispatcher.BeginInvoke(() => MessageBox.Show("#"+ck.Name +"# = "+ck.Value));
                        }
            */
            using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
            {
                string results = httpwebStreamReader.ReadToEnd();
                //MessageBox.Show(results.ToString() //-- on another thread!
                Dispatcher.BeginInvoke(() => ResponseProcessing(results.ToString()));
            }
            myResponse.Close();
        }

        private void ResponseProcessing(String response)
        {

           //response = Regex.Replace(response, "\\", "");
           //response = Regex.Replace(response, "[", "");
          // response = Regex.Replace(response, "]", "");
           // response = response.Substring(1, response.Length - 1);
         // response = response.Remove(1);
          // response = "@" + response;
           
           // response = Regex.Replace(response, " ", "#");
           // response = Regex.Replace(response, @"#<div#class=#setMK#><div#class=#projectMK#><img#src=#public/media/project/project_img_[\d]+.png##alt=#", "|");
           // response = Regex.Replace(response, @"<div[*]alt=", "|");
          //  JsonSerializer serializer = new JsonSerializer();
          //  serializer.Serialize(,testproject)

            response = @"{""responseData"":  [{""project_id"":117,""0"":117,""favorite"":false,""1"":false,""name"":""MASI Interns"",""2"":""MASI Interns"",""description"":""We are going to improve and code TUZZit to make it so magic"",""3"":""We are going to improve and code TUZZit to make it so magic"",""visibility"":2,""4"":2,""lat"":""50.2123358"",""5"":""50.2123358"",""long"":""5.3303156"",""6"":""5.3303156"",""project_hashcode"":""51724142776147594a6b32766b42384b3746306535513d3d"",""project_path"":""http:\/\/dev.tuzzit.com\/project\/51724142776147594a6b32766b42384b3746306535513d3d"",""has_warning"":true,""project_img"":""public\/media\/project\/project_img_117.png"",""mail_count"":1},{""project_id"":4242,""0"":117,""favorite"":false,""1"":false,""name"":""MASI Interns"",""2"":""MASI Interns"",""description"":""We are going to improve and code TUZZit to make it so magic"",""3"":""We are going to improve and code TUZZit to make it so magic"",""visibility"":2,""4"":2,""lat"":""50.2123358"",""5"":""50.2123358"",""long"":""5.3303156"",""6"":""5.3303156"",""project_hashcode"":""51724142776147594a6b32766b42384b3746306535513d3d"",""project_path"":""http:\/\/dev.tuzzit.com\/project\/51724142776147594a6b32766b42384b3746306535513d3d"",""has_warning"":true,""project_img"":""public\/media\/project\/project_img_117.png"",""mail_count"":1}]}";
            JObject o = JObject.Parse(response);
            List<TestProject> listProject = new List<TestProject>();

            foreach (JObject tmp in o["responseData"])
            {
                TestProject vartmp = new TestProject(
                        (string)tmp.SelectToken("project_id") ,
                        (bool)tmp.SelectToken("favorite") ,
                        (string)tmp.SelectToken("name") ,
                        (string)tmp.SelectToken("description"), 
	                    (int)tmp.SelectToken("visibility") ,
                        (string)tmp.SelectToken("lat"), 
                        (string)tmp.SelectToken("long") ,
                        (string)tmp.SelectToken("project_hashcode") ,
                        (string)tmp.SelectToken("project_path") ,
                        (bool)tmp.SelectToken("has_warning"), 
                        (string)tmp.SelectToken("project_img"), 
                        (int)tmp.SelectToken("mail_count")
               );


                listProject.Add(vartmp);
                listProject.to
            }

           
            string something = (string)o["responseData"][0]["project_id"] +" "+ (string)o["responseData"][1]["project_id"];
            MessageBox.Show(something);
        
        }
 






        
    }
}